package com.peenongsaban.freelance.service;

import java.util.Vector;

import com.peenongsaban.freelance.bean.AptitudeTypeBean;
import com.peenongsaban.freelance.bean.PaymentOfServiceBean;
import com.peenongsaban.freelance.bean.PostJobBean;
import com.peenongsaban.freelance.bean.PostServiceBean;
import com.peenongsaban.freelance.dao.HomePageDAO;
import com.peenongsaban.freelance.dao.impl.HomePageDAOImpl;

/*	List aptitude type of job and service 
 *  List urgent job
 *  List most popular job
 *  List most popular service
 * */
public class HomePageSerivce {
	
	public Vector<AptitudeTypeBean> getListAptitudeType() {
		HomePageDAO dao = new HomePageDAOImpl();
		Vector<AptitudeTypeBean> vector = dao.listAptitudeType();
		return vector;
	}
	
	public Vector<PostJobBean> getListJobUrgent() {
		HomePageDAO dao = new HomePageDAOImpl();
		Vector<PostJobBean> vector = dao.listJobUrgent();
		return vector;
	}
	
	public Vector<PostJobBean> getListJobMostPopular() {
		HomePageDAO dao = new HomePageDAOImpl();
		Vector<PostJobBean> vector = dao.listJobMostPopular();
		return vector;
	}

	public Vector<PostServiceBean> getListServiceMostPopular() {
		HomePageDAO dao = new HomePageDAOImpl();
		Vector<PostServiceBean> vector = dao.listServiceMostPopular();
		double minPriceMoney = 0.0;
		double maxPriceMoney = 0.0;
		double minPriceCoin = 0.0;
		double maxPriceCoin = 0.0;

		for (int i = 0; i < vector.size(); i++) {
			String seviceDetail = vector.get(i).getService()
					.getDescriptionOfService();
			if (seviceDetail.length() >= 100) {
				vector.get(i)
						.getService()
						.setDescriptionOfService(
								seviceDetail.substring(0, 100) + "...");
			} else {
				vector.get(i).getService()
						.setDescriptionOfService(seviceDetail + "...");
			}
			Vector<PaymentOfServiceBean> paymentVector = new Vector<PaymentOfServiceBean>();
			paymentVector = vector.get(i).getService()
					.getListPaymentOfService();
			for (int j = 0; j < paymentVector.size(); j++) {
				if (paymentVector.get(j).getPaymentType().getPaymentTypeID() == 1) {
					if (paymentVector.get(j).getPrice() > maxPriceMoney) {
						maxPriceMoney = paymentVector.get(j).getPrice();
					} else if (paymentVector.get(j).getPrice() < minPriceMoney) {
						minPriceMoney = paymentVector.get(j).getPrice();
					}
					if (minPriceMoney == 0.0) {
						vector.get(i).getService()
								.setPriceRange(maxPriceMoney + " บาท");
					} else {
						vector.get(i)
								.getService()
								.setPriceRange(
										minPriceMoney + " - " + maxPriceMoney
												+ " บาท");
					}
					break;
				} else if (paymentVector.get(j).getPaymentType()
						.getPaymentTypeID() == 2) {
					if (paymentVector.get(j).getPrice() > maxPriceCoin) {
						maxPriceCoin = paymentVector.get(j).getPrice();
					} else if (paymentVector.get(j).getPrice() < minPriceCoin) {
						minPriceCoin = paymentVector.get(j).getPrice();
					}
					if (minPriceCoin == 0) {
						vector.get(i).getService()
								.setPriceRange(maxPriceCoin + " เหรียญ");
					} else {
						vector.get(i)
								.getService()
								.setPriceRange(
										minPriceCoin + " - " + maxPriceCoin
												+ " เหรียญ");
					}
					break;
				} else if (paymentVector.get(j).getPaymentType()
						.getPaymentTypeID() == 3) {
				}
			}
		}
		return vector;
	}
}
